  package com.topone.controller;

  import com.lly835.bestpay.model.PayResponse;
  import com.topone.Enum.ResultEnum;
  import com.topone.Exception.FanZoneException;
  import com.topone.Form.CouponOrderForm;
  import com.topone.dto.OrderDTO;
  import com.topone.service.OrderService;
  import com.topone.service.PayService;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.*;
  import org.springframework.web.servlet.ModelAndView;

  import javax.validation.Valid;
  import java.util.Map;


  @Controller
  @RequestMapping("/pay")
  @Slf4j
  public class PayController {
      @Autowired
      private OrderService orderService;

      @Autowired
      private PayService payService;

      @GetMapping("/create")
      public ModelAndView create(@RequestParam("orderId") String orderId,
                                 @RequestParam("returnUrl") String returnUrl,
                                 Map<String,Object> map){

          // 1.查询订单
          OrderDTO orderDTO=orderService.findOne(orderId);
          if(orderDTO==null){
              throw new FanZoneException(ResultEnum.ORDER_NOT_EXIST);
          }
          //2.发起支付
          PayResponse payResponse= payService.create(orderDTO);
          map.put("payResponse",payResponse);
          map.put("returnUrl",returnUrl);
          return new ModelAndView("pay/create",map);
      }

      /**
       * 微信异步通知
       * @param notifyData
       *
       * 支付成功后微信自动调用该方法在，配置文件中设置该接口
       */
      @PostMapping("/notify")
      public ModelAndView notify(@RequestBody String notifyData){
          payService.notify(notifyData);

          //返回给微信处理结果
          return new ModelAndView("pay/success");
      }

      @PostMapping("/orderCreate")
      public ModelAndView orderCreate(@Valid CouponOrderForm orderForm,
                                      @RequestParam("returnUrl") String returnUrl,
                                      BindingResult bindingResult,
                                      Map<String, Object> map) {
          if (bindingResult.hasErrors()) {
              log.error("【创建订单】参数不正确orderForm={}", orderForm);
              throw new FanZoneException(ResultEnum.PARAM_ERROR.getCode(),
                      bindingResult.getFieldError().getDefaultMessage());

          }
          OrderDTO orderDTO = new OrderDTO();
          try {
              log.info(">>>>>>>>>>>>>>>it is other ways>>>>>>>>>>>>>>>>>>>");
              orderDTO = orderService.create(orderForm);
              //2.发起支付
              PayResponse payResponse = payService.create(orderDTO);
              map.put("payResponse", payResponse);
              map.put("returnUrl", returnUrl);
          } catch (FanZoneException e) {
              log.error("【创建订单】创建订单失败 e={}", e.fillInStackTrace());
              throw new FanZoneException(ResultEnum.ORDER_CREATE_ERROR);
          }

          return new ModelAndView("pay/create", map);
      }


  }